package com.aat.dxfy.masterw;

import java.util.Map;
import java.util.Set;

public class Test {

	public static void main(String[] args) {
		long l1 = System.currentTimeMillis();
		test1();
		long l2 = System.currentTimeMillis();
		test2();
		long l3 = System.currentTimeMillis();
		System.out.println("M:"+(l2-l1)+"   p:"+(l3-l2));
		System.out.println(Runtime.getRuntime().availableProcessors());
	}

	public static void test2() {
		int sum = 0;
		for (int i = 0; i < 1000; i++) {
			sum += i * i * i;
		}
		System.out.println("= " + sum);
	}

	public static void test1() {
		Master m = new Master(new PlusWoker(), 8);
		for (int i = 0; i < 1000; i++) {
			m.submit(i);
		}

		m.execute();
		int re = 0;
		Map<String, Object> resMap = m.getResultMap();
		while (resMap.size() > 0 || !m.isComplete()) {
			Set<String> keys = resMap.keySet();
			String key = null;
			for (String k : keys) {
				key = k;
				break;
			}
			Integer i = null;
			if (key != null)
				i = (Integer) resMap.get(key);
			if (i != null) {
				re += i;
			}
			if (null != key) {
				resMap.remove(key);
			}

		}
		System.out.println("= " + re);
	}

}
